package cn.edkuir.member.service.impl;

import cn.edkuir.member.entity.MemberLevelEntity;
import cn.edkuir.member.excepton.PhoneExistException;
import cn.edkuir.member.excepton.UsernameExistException;
import cn.edkuir.member.service.MemberLevelService;
import cn.edkuir.member.vo.MemberLoginVo;
import cn.edkuir.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edkuir.common.utils.PageUtils;
import cn.edkuir.common.utils.Query;

import cn.edkuir.member.dao.MemberDao;
import cn.edkuir.member.entity.MemberEntity;
import cn.edkuir.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    private MemberLevelService memberLevelService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }


    //会员注册
    @Override
    public void register(MemberRegisterVo memberRegisterVo) {
        MemberEntity memberEntity = new MemberEntity();
        //设置默认等级
        MemberLevelEntity memberLevelEntity = memberLevelService.getOne(
                new QueryWrapper<MemberLevelEntity>().eq("default_status", 1));
        memberEntity.setLevelId(memberLevelEntity.getId());
        memberEntity.setPassword(memberRegisterVo.getPassword());
        memberEntity.setNickname(memberEntity.getUsername());

        //检查用户名和手机号是否唯一
        checkUniqueMobile(memberRegisterVo.getPhone());
        checkUniqueUsername(memberRegisterVo.getUsername());

        memberEntity.setMobile(memberRegisterVo.getPhone());
        memberEntity.setUsername(memberRegisterVo.getUsername());

        //密码加密存储
        //盐值加密: md5 + 盐值
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(memberRegisterVo.getPassword());
        memberEntity.setPassword(encode);

        baseMapper.insert(memberEntity);
    }


    @Override
    public void checkUniqueMobile(String phone) {
        Integer mobile = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(mobile > 0) {
            throw new PhoneExistException();
        }
    }


    @Override
    public void checkUniqueUsername(String username) {
        Integer count = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if(count > 0) {
            throw new UsernameExistException();
        }
    }


    //登录
    @Override
    public MemberEntity login(MemberLoginVo memberLoginVo) {
        String loginAccount = memberLoginVo.getLoginAccount();
        String password = memberLoginVo.getPassword();
        //1.数据库查询账号
        MemberEntity memberEntity = baseMapper.selectOne(
                new QueryWrapper<MemberEntity>().eq("username", loginAccount)
                        .or().eq("mobile", loginAccount)
        );
        if(memberEntity == null) {
            //登录失败
            return null;
        }
        //数据库存储的密码字段
        String passwordDB = memberEntity.getPassword();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(password, passwordDB);
        if(!matches) {
            //密码不匹配
            return null;
        }

        return memberEntity;
    }

}